// import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken, setRefreshToken, getRefreshToken, removeRefreshToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { loginByPassword, getUserInfo, getTokenByRefreshToken } from '@/api/upms/user'

const getDefaultState = () => {
  return {
    token: getToken(),
    refreshToken: getRefreshToken(),
    realName: '', // 真实姓名
    avatar: '', // 头像
    deptName: '', // 部门名称
    phone: '', // 手机号
    jobName: '', // 岗位名称
    gender: '', // 性别
    email: '', // 邮箱
    roleKey: [] // 角色key
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_REFRESH_TOKEN: (state, refreshToken) => {
    state.refreshToken = refreshToken
  },
  SET_REALNAME: (state, realName) => {
    state.realName = realName
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_DEPTNAME: (state, deptName) => {
    state.deptName = deptName
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone
  },
  SET_JOBNAME: (state, jobName) => {
    state.jobName = jobName
  },
  SET_GENDER: (state, gender) => {
    state.gender = gender
  },
  SET_EMAIL: (state, email) => {
    state.email = email
  },
  SET_ROLEKEY: (state, roleKey) => {
    state.roleKey = roleKey
  }
}

const actions = {
  // 用户登录
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      loginByPassword({ username: username.trim(), password: password }).then(response => {
        if (response.code === 0) {
          const { data } = response
          commit('SET_TOKEN', data.token)
          commit('SET_REFRESH_TOKEN', data.refreshToken)
          setToken(data.token)
          setRefreshToken(data.refreshToken)
          resolve()
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 根据刷新token获取token
  getTokenByRefreshToken({ commit, state }) {
    return new Promise((resolve, reject) => {
      getTokenByRefreshToken({}).then(resp => {
        if (resp.code === 0) {
          const { data } = resp
          commit('SET_TOKEN', data.token)
          commit('SET_REFRESH_TOKEN', data.refreshToken)
          setToken(data.token)
          setRefreshToken(data.refreshToken)
          resolve()
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getUserInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('登录失败，请重试')
        }
        const { avatar, deptName, email, gender, jobName, phone, realName, roleKey } = data
        commit('SET_REALNAME', realName)
        commit('SET_AVATAR', avatar)
        commit('SET_DEPTNAME', deptName)
        commit('SET_PHONE', phone)
        commit('SET_JOBNAME', jobName)
        commit('SET_GENDER', gender)
        commit('SET_EMAIL', email)
        commit('SET_ROLEKEY', roleKey)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      removeToken() // must remove  token  first
      removeRefreshToken()
      resetRouter()
      commit('RESET_STATE')
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      // 删除token
      removeToken()
      removeRefreshToken()
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

